﻿using CIS.Web.Common;
using System;
using System.Linq;
using System.Web.Mvc;

namespace CIS.Web.Binders
{
	public class ArrayBind<T> : IModelBinder
	{
		public object BindModel(ControllerContext controllerContext,
							ModelBindingContext bindingContext)
		{
			if (controllerContext.HttpContext.Request.Form.Count > 0 &&
				controllerContext.HttpContext.Request.Form.AllKeys.Contains(bindingContext.ModelName))
			{
				return controllerContext.HttpContext.Request.Form
					    .GetValues(bindingContext.ModelName)[0].Split(ConstValues.chrComma)
					    .Select(x => (T)Convert.ChangeType(x, typeof(T))).ToArray();
			}
			return null;
		}
	}
}